package interactor;

import context.*;

import mainFrame.DrawPanel;

import java.awt.Graphics2D;
import java.awt.image.ImageObserver;

import object2D.PathObject;

import org.json.JSONException;

import util.PixelPoint;

// ********************************************************************
// *** CLASS: DrawPolygonInteractor
// ********************************************************************
public class DrawPolygonInteractor extends Interactor implements Context.Constants
{ //private static final boolean DEBUG=true; // Overriding Context.Constants.DEBUG
  static final String TAG="interactor.DrawPolygonInteractor";

  public String getMouseModeMnemonic() { return("DrawPolygon"); }
  private PathObject polygon; // !null when drawing

  // ******************************************************************
  // *** CONSTRUCTOR: DrawPolygonInteractor
  // ******************************************************************
  public DrawPolygonInteractor(DrawPanel drawPanel)
  { super(drawPanel,DRAW_POLYGON); }
  
  // ******************************************************************
  // *** METHOD: paintMouseEffect
  // ******************************************************************
  public void paintMouseEffect(Graphics2D g,ImageObserver imageObserver)
  {	if(polygon!=null) polygon.paint(g,imageObserver); }

  // ******************************************************************
  // *** METHOD: mouseDragged
  // ******************************************************************
  // Invoked when a mouse button is pressed and then dragged.
  protected void mouseDragged() { }

  // ******************************************************************
  // *** METHOD: mouseMoved
  // ******************************************************************
  // Invoked when the mouse has been moved (with no buttons down).
  protected void mouseMoved() {}

  // ******************************************************************
  // *** METHOD: mousePressed
  // ******************************************************************
  // Invoked when a mouse button has been pressed.
  protected void mouseLeftPressed(PixelPoint p) {}
  protected void mouseRightPressed(PixelPoint p) {}

  // ******************************************************************
  // *** METHOD: mouseReleased
  // ******************************************************************
  // Invoked when a mouse button has been released.
  protected void mouseLeftReleased(PixelPoint p)
  { if(polygon==null)
    { try { polygon=new PathObject(drawPanel.getObjectModel(),p); }
	  catch(JSONException e) { e.printStackTrace(); }
    }
	else polygon.add(p);
	drawPanel.repaint();
  }
  protected void mouseRightReleased(PixelPoint p)
  { if(polygon!=null)
	{ if(polygon.numberOfPoints()>2) drawObjectDone(polygon,"POLYGON");
	  polygon=null;
	}
  }

}